import numpy as np
from PIL import Image
import scipy.signal as signal
im = Image.open('1.png')
data=[]
width,height=im.size
#读取图像像素值
for h in range(height):
    row=[]
    for w in range(width):
        value = im.getpixel((w,h))
        row.append(value)
    data.append(row)
#二维中值滤波
data = np.float32(data)
data = signal.medfilt2d(data,(5,5))
#创建并保存结果图像
for h in range(height):
    for w in range(width):
        im.putpixel((w,h),int(data[h][w]))
        
im.save('reult.png')

